using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/****************************************************
// 功能：
// | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
// | Copyright (c) 2021-2025 聊来 All rights reserved.
// | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">聊来</a> )
// | @Author: 贯彻己道的云 <18970881148@qq.com>
// | DateTime: 2025/3/26 21:49:39
*****************************************************/

public class LoadHeroWindow : BaseWindow
{
    private Transform blueTeamRoot;
    private Transform redTeamRoot;
    private Canvas canvas;
    
    private readonly List<Text> textPercentList = new List<Text>();

    private void Awake()
    {
        blueTeamRoot = transform.Find("BlueTeamRoot").GetComponent<Transform>();
        redTeamRoot = transform.Find("RedTeamRoot").GetComponent<Transform>();
        canvas = transform.parent.GetComponent<Canvas>();
    }


    protected override void InitWindows()
    {
        base.InitWindows();

        // 不执行这代码试下看，有意想不到的显示效果（ScreenTapFx）
        canvas.renderMode = RenderMode.ScreenSpaceOverlay;
        
        
        HeroController.Instance.loadHeroWindow = this;
        audioManager.PlayUIAudio("load");

        var heroList = model.battleHero;
        var count = heroList.Count / 2;
        
        for (var i = 0; i < Constants.NUM_FIVE; i++)
        {
            var player = blueTeamRoot.GetChild(i);
            if (i < count)
            {
                SetActive(player);
                var heroConfig = resource.GetUnitConfigData(heroList[i].heroId);
                SetSprite(FindChildTransform(player.transform, "imgHero").GetComponent<Image>(), $"Images/load/{heroConfig.resName}_load");
                var textPercent = FindChildTransform(player.transform, "txtProgress").GetComponent<Text>();
                SetText(FindChildTransform(player.transform, "txtHeroName").GetComponent<Text>(), heroConfig.unitName);
                SetText(FindChildTransform(player.transform, "txtPlayerName").GetComponent<Text>(), heroList[i].userName);
                textPercentList.Add(textPercent);
                SetText(textPercent, "0%");
            }
            else
            {
                SetActive(player.gameObject, false);
            }
        }
        
        for (var i = 0; i < Constants.NUM_FIVE; i++)
        {
            var player = redTeamRoot.GetChild(i);
            if (i < count)
            {
                SetActive(player);
                var heroConfig = resource.GetUnitConfigData(heroList[i + count].heroId);
                SetSprite(FindChildTransform(player.transform, "imgHero").GetComponent<Image>(), $"Images/load/{heroConfig.resName}_load");
                var textPercent = FindChildTransform(player.transform, "txtProgress").GetComponent<Text>();
                SetText(FindChildTransform(player.transform, "txtHeroName").GetComponent<Text>(), heroConfig.unitName);
                SetText(FindChildTransform(player.transform, "txtPlayerName").GetComponent<Text>(), heroList[i + count].userName);
                textPercentList.Add(textPercent);
                SetText(textPercent, "0%");
            }
            else
            {
                SetActive(player.gameObject, false);
            }
        }
    }

    /*protected override void ClearWindows()
    {
        base.ClearWindows();
        
        // 销毁了跟HeroController逻辑层的关系，RefreshProgressData会失效
        // HeroController.Instance.loadHeroWindow = null;
    }*/

    /// <summary>
    /// 刷新战斗地图加载进度
    /// </summary>
    /// <param name="percentList"></param>
    public void RefreshProgressData(List<int> percentList)
    {
        for(var i = 0; i < percentList.Count; i++) {
            textPercentList[i].text = percentList[i] + "%";
        }
    }
}
